package com.rjkj.medicalwaste.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.rjkj.medicalwaste.R
import com.rjkj.medicalwaste.bean.*
import com.rjkj.medicalwaste.databinding.ItemBoxListBinding
import com.rjkj.medicalwaste.utils.TimeUtils
import java.text.ParsePosition
import java.text.SimpleDateFormat
import java.util.*

class BoxListAdapter(
    var fixBoxCode: Boolean,
    var list: List<GarbageInBoxBean>,
    private val context: Context,
    private val itemClick: ItemClick<GarbageInBoxBean>,
    private val printClick: ItemDataClick<GarbageInBoxBean>,
    private val invalidClick: InvalidClick
) : RecyclerView.Adapter<BoxListAdapter.ViewHolder>() {
    class ViewHolder(private val binding: ItemBoxListBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(
            fixBoxCode: Boolean,
            context: Context,
            item: GarbageInBoxBean,
            itemClick: ItemClick<GarbageInBoxBean>,
            printClick: ItemDataClick<GarbageInBoxBean>,
            invalidClick: InvalidClick
        ) {
            item.garbageType.let {
                binding.tvType.setTextColor(ContextCompat.getColor(context, it.colorId))
                binding.tvType.text = it.typeName
            }
            val hmTime = TimeUtils.getHMTime(transToTimeStamp(item.garbageBoxTime))
            binding.tvContent.text = hmTime
            binding.root.setOnClickListener {
                itemClick.onClicked(it, item)
            }
            binding.fixCode = fixBoxCode
            if (!fixBoxCode) {
                binding.btPrint.setOnClickListener(object :
                    OnMultiClickDataListener<GarbageInBoxBean>(item) {
                    override fun onMultiClick(data: GarbageInBoxBean, v: View?) {
                        printClick.onClicked(data)
                    }

                    override fun onInvalidClick(long: Long) {
                        invalidClick.onInvalidClick(long)
                    }

                })
            }

            binding.executePendingBindings()
        }

        @SuppressLint("SimpleDateFormat")
        fun transToTimeStamp(date: String): Date? {
            return SimpleDateFormat("yy-MM-DD hh:mm:ss").parse(date)
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val test = list[position]
        holder.bind(fixBoxCode, context, test, itemClick, printClick, invalidClick)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemBoxListBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }
}